﻿using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Excel_WR
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.FileName.Text = "Excel模板";
            this.ExcelCol.Text = "Mol;ProjectId;QuatePrice;CustomId;WuxiId";
        }

        /// <summary>
        /// 选择存放路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderPath = new FolderBrowserDialog();
            if (folderPath.ShowDialog() == DialogResult.OK)
            {
                this.FolderPath.Text = folderPath.SelectedPath;
            }
        }

        /// <summary>
        /// 生产Excel模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Aspose.Cells.License li = new Aspose.Cells.License();
            li.SetLicense("Aid/License.lic");
            Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
            Worksheet ws = wk.Worksheets[0];
            for (int i = 0; i < 10; i++)
            {
                if (i == 0)
                {
                    List<string> list = this.ExcelCol.Text.Split(';').ToList();
                    for (int j = 0; j < list.Count; j++)
                    {
                        ws.Cells.Rows[i][j].Value = list[j];
                    }
                    continue;
                }
                for (int j = 0; j < 9; j++)
                {
                    ws.Cells.Rows[i][j].Value = (i) + "*" + (j + 1) + "=" + (i) * (j + 1);
                }
            }
            string allPath = this.FolderPath.Text + "\\" + this.FileName.Text + ".xlsx";
            wk.Save(allPath);
            MessageBox.Show($"生成{allPath}成功！");
        }

        /// <summary>
        /// 选择需要打开的文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "请选择文件夹";
            fileDialog.Filter = "所有文件(*.Excel)|*.xlsx";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                this.OpenFileAllPath.Text = fileDialog.FileName;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Aspose.Cells.License li = new Aspose.Cells.License();
            li.SetLicense("Aid/License.lic");
            Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook(this.OpenFileAllPath.Text);
            Worksheet ws = wk.Worksheets[0];
            dt = ws.Cells.ExportDataTable(0, 0, ws.Cells.MaxColumn, ws.Cells.MaxRow);
        }
    }
}
